using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
namespace HKB.Data.Bll
{
	public class UserGiftCardBll
	{
		private static UserGiftCardBll _instance = null;
		private readonly UserGiftCardDal dal = new UserGiftCardDal();
		public static UserGiftCardBll instance
		{
			get
			{
				if (UserGiftCardBll._instance == null)
				{
					UserGiftCardBll._instance = new UserGiftCardBll();
				}
				return UserGiftCardBll._instance;
			}
		}
		public bool Exists(string CardNumber)
		{
			return this.dal.Exists(CardNumber);
		}
		public void Add(UserGiftCardModel model)
		{
			this.dal.Add(model);
		}
		public bool Update(UserGiftCardModel model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(string CardNumber)
		{
			return this.dal.Delete(CardNumber);
		}
		public UserGiftCardModel GetModel(string CardNumber)
		{
			return this.dal.GetModel(CardNumber);
		}
		public UserGiftCardModel GetModelByCache(string CardNumber)
		{
			string cacheKey = "UserGiftCardModel_" + CardNumber;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(CardNumber);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (UserGiftCardModel)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<UserGiftCardModel> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<UserGiftCardModel> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<UserGiftCardModel> list = new System.Collections.Generic.List<UserGiftCardModel>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					UserGiftCardModel userGiftCardModel = new UserGiftCardModel();
					userGiftCardModel.CardNumber = dt.Rows[i]["CardNumber"].ToString();
					userGiftCardModel.Password = dt.Rows[i]["Password"].ToString();
					userGiftCardModel.FestivalName = dt.Rows[i]["FestivalName"].ToString();
					if (dt.Rows[i]["FaceValue"] != null && dt.Rows[i]["FaceValue"].ToString() != "")
					{
						userGiftCardModel.FaceValue = new decimal?(decimal.Parse(dt.Rows[i]["FaceValue"].ToString()));
					}
					if (dt.Rows[i]["Balance"] != null && dt.Rows[i]["Balance"].ToString() != "")
					{
						userGiftCardModel.Balance = new decimal?(decimal.Parse(dt.Rows[i]["Balance"].ToString()));
					}
					if (dt.Rows[i]["CouponAmount"] != null && dt.Rows[i]["CouponAmount"].ToString() != "")
					{
						userGiftCardModel.CouponAmount = new decimal?(decimal.Parse(dt.Rows[i]["CouponAmount"].ToString()));
					}
					userGiftCardModel.Picture = dt.Rows[i]["Picture"].ToString();
					userGiftCardModel.Title = dt.Rows[i]["Title"].ToString();
					if (dt.Rows[i]["BuyTime"] != null && dt.Rows[i]["BuyTime"].ToString() != "")
					{
						userGiftCardModel.BuyTime = new System.DateTime?(System.DateTime.Parse(dt.Rows[i]["BuyTime"].ToString()));
					}
					if (dt.Rows[i]["BuyID"] != null && dt.Rows[i]["BuyID"].ToString() != "")
					{
						userGiftCardModel.BuyID = new int?(int.Parse(dt.Rows[i]["BuyID"].ToString()));
					}
					if (dt.Rows[i]["RecieveID"] != null && dt.Rows[i]["RecieveID"].ToString() != "")
					{
						userGiftCardModel.RecieveID = new int?(int.Parse(dt.Rows[i]["RecieveID"].ToString()));
					}
					userGiftCardModel.ReceiveEmail = dt.Rows[i]["ReceiveEmail"].ToString();
					userGiftCardModel.LeaveWords = dt.Rows[i]["LeaveWords"].ToString();
					list.Add(userGiftCardModel);
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public DataSet GetListRowid(int Top, string strWhere, string filedOrder, params string[] selectField)
		{
			return this.dal.GetListRowid(Top, strWhere, filedOrder, selectField);
		}
		public DataSet PageOrderList(int PageSize, int PageIndex, string Orderfld, int OrderType, string strWhere)
		{
			return Conn.ExecProSpPageOrder("MBF_UserGiftCard", " ROW_NUMBER() OVER (ORDER BY CardNumber ASC) rowid,*", PageSize, PageIndex, Orderfld, OrderType, strWhere);
		}
		public string ToString(UserGiftCardModel model)
		{
			string result;
			if (model == null)
			{
				result = "";
			}
			else
			{
				string text = "";
				System.Type type = model.GetType();
				System.Reflection.PropertyInfo[] properties = type.GetProperties();
				for (int i = 0; i < properties.Length; i++)
				{
					System.Reflection.PropertyInfo property = type.GetProperty(properties[i].Name);
					object value = property.GetValue(model, null);
					string text2 = (value == null) ? "" : value.ToString();
					string text3 = text;
					text = string.Concat(new string[]
					{
						text3,
						properties[i].Name,
						"|",
						text2,
						"_"
					});
				}
				if (text.Length > 0)
				{
					text = text.Substring(0, text.Length - 1);
				}
				result = text;
			}
			return result;
		}
		public UserGiftCardModel DesToString(string modelstr)
		{
			UserGiftCardModel result;
			if (modelstr == "")
			{
				result = null;
			}
			else
			{
				UserGiftCardModel userGiftCardModel = new UserGiftCardModel();
				System.Type type = userGiftCardModel.GetType();
				System.Collections.Generic.Dictionary<string, object> dictionary = new System.Collections.Generic.Dictionary<string, object>();
				System.Reflection.PropertyInfo[] properties = type.GetProperties();
				object[] array = modelstr.Split(new char[]
				{
					'_'
				});
				object[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					object obj = array2[i];
					object[] array3 = obj.ToString().Split(new char[]
					{
						'|'
					});
					dictionary[array3[0].ToString()] = array3[1];
				}
				for (int j = 0; j < properties.Length; j++)
				{
					string text = properties[j].PropertyType.ToString().ToLower();
					if (dictionary[properties[j].Name].ToString() == "")
					{
						dictionary[properties[j].Name] = null;
					}
					System.Reflection.PropertyInfo property = type.GetProperty(properties[j].Name);
					object value = null;
					if (text.Contains("decimal"))
					{
						if (dictionary[properties[j].Name] != null)
						{
							value = System.Convert.ToDecimal(dictionary[properties[j].Name]);
						}
					}
					else
					{
						if (text.Contains("int32"))
						{
							if (dictionary[properties[j].Name] != null)
							{
								value = System.Convert.ToInt32(dictionary[properties[j].Name]);
							}
						}
						else
						{
							if (text.Contains("int64"))
							{
								if (dictionary[properties[j].Name] != null)
								{
									value = System.Convert.ToInt64(dictionary[properties[j].Name]);
								}
							}
							else
							{
								if (text.Contains("datetime") || text.Contains("date"))
								{
									if (dictionary[properties[j].Name] != null)
									{
										value = System.Convert.ToDateTime(dictionary[properties[j].Name]);
									}
								}
								else
								{
									value = dictionary[properties[j].Name];
								}
							}
						}
					}
					property.SetValue(userGiftCardModel, value, null);
				}
				result = userGiftCardModel;
			}
			return result;
		}
	}
}
